﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using Models;

namespace DAL
{
    public class SetBasicInfoDAL
    {
#pragma warning disable CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
        string conn = ConfigurationSettings.AppSettings["con"].ToString();
#pragma warning restore CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
        //获取已存菜单设置信息
        public string getInfo(string name)
        {
            SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@name",name)
            };
            string sqlstr = "select isnull(text,'') from wechatinfo where name = @name";
            return SqlHelper.ExecuteScalar(conn,CommandType.Text,sqlstr,sp).ToString();
        }
        //设置菜单信息更新至数据库
        public int setInfo(string name, string text)
        {
            SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@name",name),
                new SqlParameter("@text",text)
            };
            string sqlstr = "if not exists(select 1 from wechatinfo where name=@name) begin insert into wechatinfo values(@name,@text) end else begin update wechatinfo set text = @text where name = @name end";
            return SqlHelper.ExecuteNonQuery(conn,CommandType.Text,sqlstr,sp);
        }

        /// <summary>
        /// 获取微信基础设置信息
        /// </summary>
        /// <returns></returns>
        public DataTable getWechatBaseInfo()
        {
            SqlParameter[] sp = new SqlParameter[]{
            };
            string sqlstr = "select * from wechatBaseInfo";
            return SqlHelper.ExecuteDataSet(conn,CommandType.Text,sqlstr,sp).Tables[0];
        }
        /// <summary>
        /// 设置微信基础设置信息
        /// </summary>
        /// <param name="wb"></param>
        public int setWechatBaseInfo(WechatBaseInfo wb)
        {
            SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@token",wb.token),
                new SqlParameter("@EncodingAESKey",wb.EncodingAESKey),
                new SqlParameter("@appId",wb.appId),
                new SqlParameter("@appSecret",wb.appSecret)
            };
            string sqlstr = "if not exists(select 1 from wechatBaseInfo)begin insert into wechatBaseInfo values(@token,@EncodingAESKey,@appId,@appSecret) end else begin update wechatBaseInfo set token=@token,EncodingAESKey=@EncodingAESKey,appId=@appId,appSecret=@appSecret end";
            return SqlHelper.ExecuteNonQuery(conn,CommandType.Text,sqlstr,sp);
        }
    }
}
